package j.framework.tags.form;

import javax.servlet.jsp.JspException;

/**
 * 所有的input标签及textarea标签都继承自此类
 * User: jason
 * Date: 2014/12/18 9:38
 */
public abstract class BaseInputTag extends BaseFormControl {
    private String onClick;
    private Object value;
    private String placeHolder;
    private Integer maxLength;

    public abstract String getInputType();

    /**
     * 给标签增加value属性
     * @param writer
     * @param value
     * @throws JspException
     */
    public void writeInputTagValue(FormTagWriter writer,Object value) throws JspException{
        if(null == value){
            value = getFormDataValue();
        }
        if(null!=value){
            writer.writeFormTagProperty("value",value.toString());
        }
    }

    @Override
    public void writeFormTagBegin(FormTagWriter writer) throws JspException {
        writer.beginTag("input");
    }

    @Override
    public void writeFormTagProperties(FormTagWriter writer) throws JspException {
        writer.writeFormTagProperty("type", getInputType());
        writeInputTagValue(writer,value);
        if(null!=maxLength && maxLength>0){
            writer.writeFormTagProperty("maxlength",maxLength.toString());
        }
        writer.writeFormTagProperty("onclick", onClick);
        writer.writeFormTagProperty("placeholder", placeHolder);
    }

    @Override
    public void endBeginTag(FormTagWriter writer)  throws JspException{
        writer.closeBlankTag();
    }

    public void setOnClick(String onClick) {
        this.onClick = onClick;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public void setPlaceHolder(String placeHolder) {
        this.placeHolder = placeHolder;
    }

    public void setMaxLength(Integer maxLength) {
        this.maxLength = maxLength;
    }
}
